package com.tanhua.server.service;

import com.tanhua.domain.mongo.*;
import com.tanhua.dubbo.api.mongo.*;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.vo.OptionsVo;
import com.tanhua.vo.PaperVo;
import com.tanhua.vo.QuestionVo;
import com.tanhua.vo.ResultVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TestSoulService {

    @Reference
    private TestSoulApi testSoulApi;

    @Reference
    private TestPaperApi paperApi;

    @Reference
    private TestQuestionApi testQuestionApi;

    @Reference
    private UserTestApi userTestApi;

    @Reference
    private TestOptionApi optionApi;

    @Reference
    private TestResultApi resultApi;


    public ResponseEntity testSoulList() {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //去后台查出3套题
        List<TestPaper> paperList = paperApi.findAll();
        List<PaperVo> voList = new ArrayList<>();
        //这是往每套题塞问题,塞选项
        for (TestPaper paper : paperList) {
            //第几套题
            Integer type = paper.getType();
            PaperVo vo = new PaperVo();
            BeanUtils.copyProperties(paper, vo);
            vo.setId(paper.getId().toString());
            List<QuestionVo> qvoList = new ArrayList<>();
            List<TestQuestion> questionList = testQuestionApi.findByPaperId(type);
            for (TestQuestion question : questionList) {
                String questionId = question.getId().toString();
                //从数据库查出本套试题所有选项
                List<TestOptions> options = optionApi.findOptionByQuestionId(questionId, type);
                List<OptionsVo> oVos = new ArrayList<>();
                for (TestOptions option : options) {
                    OptionsVo oVo = new OptionsVo();
                    oVo.setId(option.getId().toString());
                    oVo.setOption(option.getOption());
                    oVos.add(oVo);
                }
                QuestionVo qvo = new QuestionVo();
                qvo.setId(question.getId().toString());
                qvo.setQuestion(question.getQuestion());
                qvo.setOptions(oVos);
                qvoList.add(qvo);
            }
            vo.setQuestions(qvoList);
            //根据用户id和试题类型查询最新报告id
            //没有报告id的话,前台就会显示开始测试,有的话就会显示查看报告
            UserTest userTest = userTestApi.findReportIdByUserId(userId, type);
            if (null != userTest) {
                if (StringUtils.isNotBlank(userTest.getReportId())) {
                    vo.setReportId(userTest.getReportId());
                }
            }

            voList.add(vo);
        }
        System.out.println(voList);
        return ResponseEntity.ok(voList);
    }

    //查看测灵魂结果
    public ResponseEntity testSoulListReport(String reportId) {
        ResultVo resultVo = new ResultVo();
        TestResult result = resultApi.findByReportId(reportId);
        resultVo.setConclusion(result.getEvaluation());
        resultVo.setCover(result.getCover());
        //维度  假数据仅供测试使用
        List<Dimensions> dimsList = new ArrayList<>();
        Dimensions di = new Dimensions();
        di.setKey("外向");
        di.setValue("80%");
        dimsList.add(di);

        Dimensions di1 = new Dimensions();
        di1.setKey("判断");
        di1.setValue("80%");
        dimsList.add(di1);

        Dimensions di2 = new Dimensions();
        di2.setKey("抽象");
        di2.setValue("80%");
        dimsList.add(di2);

        Dimensions di3 = new Dimensions();
        di3.setKey("理性");
        di3.setValue("80%");
        dimsList.add(di3);

        resultVo.setDimensions(dimsList);
        //与你相似  假数据,没有接入大数据
        List<SimilarYou> similarYouList = new ArrayList<>();
        for (int i = 3; i < 14; i++) {
            SimilarYou similarYou = new SimilarYou();
            similarYou.setUserId(i);
            similarYou.setAvatar("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_" + i + ".jpg");
            similarYouList.add(similarYou);
        }

        resultVo.setSimilarYou(similarYouList);
        System.out.println(resultVo);
        return ResponseEntity.ok(resultVo);
    }

    //测灵魂-提交试卷
    public ResponseEntity submitTest(List<Object> list) {
        Long userId = UserHolder.getUserId();
        ResultVo resultVo = new ResultVo();
        Integer sum = 0;
        String passType = null;
        //这里是因为前台传入数据太诡异,需要分割问题id和选项id
        for (Object o : list) {
            String s = o.toString();
            //想知道为啥这样切,打断点看看数据格式
            String questionId1 = s.split(",")[0];
            String questionId = questionId1.split("=")[1];
            String optionIdStr= s.split(",")[1];
            String optionIdStr1 = optionIdStr.split("=")[1];
            String optionId = optionIdStr1.split("}")[0];

            TestOptions option = optionApi.getScore(questionId, optionId);
            //获取总共答了多少分 及试卷所属类型
            sum += option.getScore();
            passType = option.getType();
        }
        System.out.println(sum);
        TestResult result = resultApi.findByScore(sum);
        BeanUtils.copyProperties(result, resultVo);
        String reportId = result.getId().toString();
        UserTest userTest = new UserTest();
        userTest.setUserId(userId);
        userTest.setPassType(passType);
        userTest.setReportId(reportId);
        //保存个人用户测试报告
        userTestApi.save(userTest);
        //判断所作试卷类型,用来解锁下一难度试卷
        switch (passType) {
            case "1":
                paperApi.updateStatus(0, 2);
            case "2":
                paperApi.updateStatus(0,3);
        }

        return ResponseEntity.ok(reportId);
    }
}
